﻿namespace GSC.WebControls
{
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.Design.WebControls;
    using System.Drawing.Design;
    using System.Drawing;
    using System.Web.UI.Design;
    using System.ComponentModel;

    /// <summary>
    /// 左边垂直菜单控件标题。
    /// </summary>
    /// <remarks>左边垂直菜单控件标题。</remarks>
    [ToolboxItem(false)]
    [PersistChildren(false)]
    //[DefaultProperty("Buttons")]
    [ParseChildren(true, "Buttons")]
    [Designer(typeof(BackgroundCenterLeftMenuTitleControlDesigner))]
    public class BackgroundCenterLeftMenuTitleControl : Panel, INamingContainer, IStateManager, ICompositeControlDesignerAccessor, IIsExpanded
    {
        #region ICompositeControlDesignerAccessor
        void ICompositeControlDesignerAccessor.RecreateChildControls()
        {
            base.ChildControlsCreated = false;
            EnsureChildControls();
        }
        #endregion
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return string.IsNullOrEmpty(this.ID) ? base.ToString() : this.ID;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            Page.RegisterRequiresControlState(this);
            base.OnInit(e);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        protected override void AddParsedSubObject(object obj)
        {
            if (obj is BackgroundCenterLeftMenuButtonControl)
                this.Buttons.Add((BackgroundCenterLeftMenuButtonControl)obj);
        }
        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls()
        {
            Controls.Clear();
            this.CreateControlHierarchy();
            base.ClearChildControlState();
        }
        #region IStateManager Members
        internal void SetDirty()
        {
            _viewState.SetDirty(true);
        }
        private bool _isTrackingViewState;
        private StateBag _viewState;
        bool IStateManager.IsTrackingViewState
        {
            get
            {
                return _isTrackingViewState;
            }
        }
        void IStateManager.LoadViewState(object state)
        {
            Pair pair = state as Pair;
            if (pair != null)
            {
                ((IStateManager)ViewState).LoadViewState(pair.First);
                ((IStateManager)this.Buttons).LoadViewState(pair.Second);
            }
        }
        object IStateManager.SaveViewState()
        {
            return new Pair(((IStateManager)ViewState).SaveViewState(), ((IStateManager)this.Buttons).SaveViewState());
        }
        void IStateManager.TrackViewState()
        {
            _isTrackingViewState = true;
            ((IStateManager)ViewState).TrackViewState();
            ((IStateManager)this.Buttons).TrackViewState();
        }
        /// <summary>
        /// 
        /// </summary>
        protected override StateBag ViewState
        {
            get
            {
                if (_viewState == null)
                {
                    _viewState = new StateBag(false);
                    if (_isTrackingViewState)
                    {
                        ((IStateManager)_viewState).TrackViewState();
                    }
                }
                return _viewState;
            }
        }
        #endregion
        private BackgroundCenterLeftMenuButtonControlCollection _buttons = null;
        /// <summary>
        /// 获取按钮集合。
        /// </summary>
        /// <remarks>获取按钮集合。</remarks>
        /// <value>WebControls.CenterLeftMenuButtonCollection</value>
        [
        PersistenceMode(PersistenceMode.InnerProperty),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
        EditorAttribute(typeof(BackgroundCenterLeftMenuButtonControlCollectionEditor), typeof(UITypeEditor)),
        NotifyParentProperty(true)
        ]
        public BackgroundCenterLeftMenuButtonControlCollection Buttons
        {
            get
            {
                if (_buttons == null)
                {
                    _buttons = new BackgroundCenterLeftMenuButtonControlCollection();
                    if (IsTrackingViewState)
                    {
                        ((IStateManager)_buttons).TrackViewState();
                    }
                }
                return _buttons;
            }
        }
        /// <summary>
        /// 获取标题折叠状态图标对象。
        /// </summary>
        /// <remarks>获取标题折叠状态图标对象。</remarks>
        /// <value>System.Web.UI.WebControls.Image</value>
        [Browsable(false)]
        public System.Web.UI.WebControls.Image Icon
        {
            get
            {
                //this.EnsureChildControls();
                return _icon;
            }
        }
        private System.Web.UI.WebControls.Image _icon;
        private System.Web.UI.WebControls.LinkButton _button;
        /// <summary>
        /// 获取标题按钮对象。
        /// </summary>
        /// <remarks>获取标题按钮对象。</remarks>
        /// <value>System.Web.UI.WebControls.Button</value>
        [Browsable(false)]
        public LinkButton Button
        {
            get
            {
                if (_button == null) _button = new LinkButton();
                return _button;
            }
        }

        private Panel _titleContainer;

        /// <summary>
        /// 获取标题容器对象。
        /// </summary>
        /// <remarks>获取标题容器对象。</remarks>
        /// <value>System.Web.UI.WebControls.Panel</value>
        [Browsable(false)]
        public System.Web.UI.WebControls.Panel TitleContainer
        {
            get
            {
                //this.EnsureChildControls();
                return _titleContainer;
            }
        }

        private Panel _contentContainer;

        /// <summary>
        /// 获取按钮容器对象。
        /// </summary>
        /// <remarks>获取按钮容器对象。</remarks>
        /// <value>System.Web.UI.WebControls.Panel</value>
        [Browsable(false)]
        public Panel ContentContainer
        {
            get
            {
                //this.EnsureChildControls();
                return _contentContainer;
            }
        }

        /// <summary>
        /// 获取或设置是否为展开状态。
        /// </summary>
        /// <remarks>获取或设置是否为展开状态。</remarks>
        /// <value>bool</value>
        [DefaultValue(false)]
        [Browsable(true)]
        [Bindable(true)]
        [Category("Behavior")]
        public bool IsExpanded
        {
            get
            {
                object o = this.ViewState["IsExpanded"];
                if (o == null) return false;
                return (bool)o;
            }
            set
            {
                this.ViewState["IsExpanded"] = value;
            }
        }
        /// <summary>
        /// 获取或设置标题名称。
        /// </summary>
        /// <remarks>获取或设置标题名称。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [Bindable(true)]
        [DefaultValue("")]
        //[Category("Appearance")]
        [Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design", typeof(UITypeEditor))]
        public string Text
        {
            get
            {
                object o = this.ViewState["Text"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["Text"] = value;
            }
        }
        /// <summary>
        /// 获取标题文字容器对象。
        /// </summary>
        /// <remarks>获取标题文字容器对象。</remarks>
        /// <value>System.Web.UI.WebControls.Panel</value>
        [Browsable(false)]
        public System.Web.UI.WebControls.Panel ButtonContainer
        {
            get
            {
                //this.EnsureChildControls();
                return _buttonContainer;
            }
        }
        /// <summary>
        /// 获取标题图标容器对象。
        /// </summary>
        /// <remarks>获取标题图标容器对象。</remarks>
        /// <value>System.Web.UI.WebControls.Panel</value>
        [Browsable(false)]
        public System.Web.UI.WebControls.Panel IconContainer
        {
            get
            {
                //this.EnsureChildControls();
                return _iconContainer;
            }
        }
        private Panel _buttonContainer;
        private Panel _iconContainer;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            this.Attributes["IsExpanded"] = this.IsExpanded.ToString();
            base.OnPreRender(e);
        }
        /// <summary>
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (Page != null)
                Page.VerifyRenderingInServerForm(this);
            base.Render(writer);
        }
        /// <summary>
        /// 获取或设置命令名，该命令名与传递给 Expanded 事件的 Button 控件相关联。
        /// </summary>
        /// <remarks>获取或设置命令名，该命令名与传递给 Expanded 事件的 Button 控件相关联。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [Bindable(true)]
        [DefaultValue("")]
        [Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design", typeof(UITypeEditor))]
        public string CommandName
        {
            get
            {
                object o = this.ViewState["CommandName"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["CommandName"] = value;
            }
        }
        /// <summary>
        /// 获取或设置可选参数，该参数与关联的 CommandName 一起被传递到 Expanded 事件。
        /// </summary>
        /// <remarks>获取或设置可选参数，该参数与关联的 CommandName 一起被传递到 Expanded 事件。</remarks>
        /// <value>string</value>
        [Browsable(true)]
        [Bindable(true)]
        [DefaultValue("")]
        [Editor("System.ComponentModel.Design.MultilineStringEditor,System.Design", typeof(UITypeEditor))]
        public string CommandArgument
        {
            get
            {
                object o = this.ViewState["CommandArgument"];
                if (o == null) return string.Empty;
                return (string)o;
            }
            set
            {
                this.ViewState["CommandArgument"] = value;
            }
        }
        /// <summary>
        /// 获取或设置标题字体原色。
        /// </summary>
        /// <remarks>获取或设置标题字体原色。</remarks>
        /// <value>System.Drawing.Color</value>
        [Browsable(true)]
        [DefaultValue(typeof(Color), "")]
        [Category("Appearance")]
        [TypeConverter(typeof(System.Web.UI.WebControls.WebColorConverter))]
        public override Color ForeColor
        {
            get
            {
                return _buttonStyle.ForeColor;
            }
            set
            {
                _buttonStyle.ForeColor = value;
            }
        }
        private Style _buttonStyle = new Style();
        /// <summary>
        /// 获取按钮的样式对象。
        /// </summary>
        /// <remarks>获取按钮的样式对象。</remarks>
        /// <value>System.Web.UI.WebControls.Style</value>
        [Browsable(false)]
        public Style ButtonStyle
        {
            get
            {
                return _buttonStyle;
            }
        }
        /// <summary>
        /// 获取标题字体对象。
        /// </summary>
        /// <remarks>获取标题字体对象。</remarks>
        /// <value>System.Web.UI.WebControls.FontInfo</value>
        [Browsable(true)]
#if ONLY_1_1
		[DefaultValue(null)]
#endif
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        [NotifyParentProperty(true)]
        [Category("Appearance")]
        public override FontInfo Font
        {
            get
            {
                if (_buttonStyle.Font.Size.IsEmpty) _buttonStyle.Font.Size = new FontUnit("12px");
                return _buttonStyle.Font;
            }
        }
        /// <summary>
        /// </summary>
        protected override string TagName
        {
            get
            {
                return "div";
            }
        }

        /// <summary>
        /// </summary>
        protected override HtmlTextWriterTag TagKey
        {
            get
            {
                return HtmlTextWriterTag.Div;
            }
        }

        /// <summary>
        /// </summary>
        protected virtual void CreateControlHierarchy()
        {
            this._titleContainer = new Panel();
            this._iconContainer = new Panel();
            this._icon = new System.Web.UI.WebControls.Image();
            this._iconContainer.Controls.Add(this._icon);
            this._buttonContainer = new Panel();
            if (_button == null)
                this._button = new LinkButton();
            this._buttonContainer.Controls.Add(this._button);
            //
            this._titleContainer.Controls.Add(this._iconContainer);
            this._titleContainer.Controls.Add(this._buttonContainer);
            //
            this._contentContainer = new Panel();
            if (!this.DesignMode)
            {
                this._titleContainer.ID = "TitleContainer";
                this._iconContainer.ID = "IconContainer";
                this._icon.ID = "Icon";
                this._buttonContainer.ID = "ButtonContainer";
                this._button.ID = "Button";
                this._contentContainer.ID = "ContentContainer";
            }
            foreach (BackgroundCenterLeftMenuButtonControl item in this.Buttons)
            {
                this._contentContainer.Controls.Add(item);
            }
            //
            this.Controls.Add(this._titleContainer);
            this.Controls.Add(this._contentContainer);
        }
    }
}